package com.zyzd.commontools.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.zyzd.commontools.AppContext;


/**
 * Created by 李宗源 on 2016/8/1.
 */
public class DeviceUtil {

    private static int screenWidth;
    private static int screenHeight;

    public static int pxToDp(float pxValue) {
        return (int) (pxValue / getDisplayMetrics().density + 0.5f);
    }

    public static int dpToPx(float dipValue) {
        return (int) (dipValue * getDisplayMetrics().density + 0.5f);
    }

    public static int pxToSp(float pxValue) {
        return (int) (pxValue / getDisplayMetrics().scaledDensity + 0.5f);
    }

    public static int spToPx(float spValue) {
        return (int) (spValue * getDisplayMetrics().scaledDensity + 0.5f);
    }

    public static DisplayMetrics getDisplayMetrics() {
        return getContext().getResources().getDisplayMetrics();
    }

    /**
     * @return 获取屏幕的宽
     */
    public static int getWidth() {
        if (screenWidth == 0) {
            screenWidth = getDisplayMetrics().widthPixels;
        }
        return screenWidth;
    }

    /**
     * @return 获取屏幕的高
     */
    public static int getHeight() {
        if (screenHeight == 0) {
            screenHeight = getDisplayMetrics().heightPixels;
        }
        return screenHeight;
    }

    public static void hideSoftKeyboard(View view) {
        if (view == null)
            return;
        View focusView = null;
        if (view instanceof EditText)//判断view是否是EditText
            focusView = view;
        Context context = view.getContext();
        if (context != null && context instanceof Activity) {//context得是Activity
            Activity activity = ((Activity) context);
            focusView = activity.getCurrentFocus();//获取当前activity中获得焦点的view
        }

        if (focusView == null) return;//当前activity中有获得焦点的view，才进行掩藏软键盘
        /*
        if (focusView.isFocusable()) {
                focusView.setFocusable(false);
                focusView.setFocusable(true);
        }
        */
        if (focusView.isFocused()) {//取消获得焦点view的获取状态
            focusView.clearFocus();
        }
        //掩藏软键盘
        InputMethodManager manager = (InputMethodManager) focusView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (manager == null) return;
        manager.hideSoftInputFromWindow(focusView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        manager.hideSoftInputFromInputMethod(focusView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    }

    public static void showSoftKeyboard(Dialog dialog) {
        Window window = dialog.getWindow();
        if (window == null) return;
        window.setSoftInputMode(4);
    }

    public static void showSoftKeyboard(View view) {
        if (view == null)
            return;

        //当view没有获得焦点就令其获取焦点
        if (!view.isFocusable())
            view.setFocusable(true);
        if (!view.isFocusableInTouchMode())
            view.setFocusableInTouchMode(true);
        if (!view.isFocused()) {
            view.requestFocus();
        }
        //显示软键盘
        InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null) return;
        inputMethodManager.showSoftInput(view, 0);
        inputMethodManager.showSoftInputFromInputMethod(view.getWindowToken(), 0);
    }

    /**
     * 切换软键盘的显示状态
     */
    public static void toogleSoftKeyboard() {
        InputMethodManager systemService = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (systemService == null) return;
        systemService.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private static Context getContext() {
        return AppContext.getContext();
    }

}
